package com.ruoyi.web.controller.fantasy;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.huanzhidadi.fantasy.bean.domain.FantasyChemistFormula;
import com.ruoyi.fantasy.domain.FantasyChemistFormulaBO;
import com.ruoyi.fantasy.domain.FantasyItemType;
import com.ruoyi.fantasy.service.IFantasyItemtypeService;
import com.ruoyi.framework.util.ShiroUtils;
import com.ruoyi.system.domain.SysUser;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.fantasy.service.IFantasyChemistFormulaService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 药剂配方Controller
 * 
 * @author wanghui
 * @date 2020-09-10
 */
@Controller
@RequestMapping("/fantasy/formula")
public class FantasyChemistFormulaController extends BaseController
{
    private String prefix = "fantasy/formula";

    @Autowired
    private IFantasyChemistFormulaService fantasyChemistFormulaService;

    @Autowired
    private IFantasyItemtypeService fantasyItemtypeService;

    @RequiresPermissions("fantasy:formula:view")
    @GetMapping()
    public String formula()
    {
        return prefix + "/formula";
    }

    /**
     * 查询药剂配方列表
     */
    @RequiresPermissions("fantasy:formula:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(FantasyChemistFormula fantasyChemistFormula)
    {
        startPage();
        List<FantasyChemistFormulaBO> list = fantasyChemistFormulaService.selectFantasyChemistFormulaList(fantasyChemistFormula);
        fantasyChemistFormulaService.transforData(list);
        return getDataTable(list);
    }

    /**
     * 导出药剂配方列表
     */
    @RequiresPermissions("fantasy:formula:export")
    @Log(title = "药剂配方", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(FantasyChemistFormula fantasyChemistFormula)
    {
        List<FantasyChemistFormulaBO> list = fantasyChemistFormulaService.selectFantasyChemistFormulaList(fantasyChemistFormula);
        ExcelUtil<FantasyChemistFormulaBO> util = new ExcelUtil<FantasyChemistFormulaBO>(FantasyChemistFormulaBO.class);
        return util.exportExcel(list, "formula");
    }

    /**
     * 新增药剂配方
     */
    @GetMapping("/add")
    public String add(ModelMap mmap) {
        List<FantasyItemType> itemTypes = fantasyItemtypeService.selectFantasyItemtypeList(null);
        mmap.addAttribute("itemTypes", itemTypes);
        return prefix + "/add";
    }

    /**
     * 新增保存药剂配方
     */
    @RequiresPermissions("fantasy:formula:add")
    @Log(title = "药剂配方", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(FantasyChemistFormula fantasyChemistFormula)
    {
        SysUser user = ShiroUtils.getSysUser();
        fantasyChemistFormula.setCreateBy(user.getUserId().toString());
        fantasyChemistFormula.setCreateTime(new Date());
        return toAjax(fantasyChemistFormulaService.insertFantasyChemistFormula(fantasyChemistFormula));
    }

    /**
     * 修改药剂配方
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") String id, ModelMap mmap)
    {
        List<FantasyItemType> itemTypes = fantasyItemtypeService.selectFantasyItemtypeList(null);
        mmap.addAttribute("itemTypes", itemTypes);
        FantasyChemistFormula fantasyChemistFormula = fantasyChemistFormulaService.selectFantasyChemistFormulaById(id);
        mmap.put("fantasyChemistFormula", fantasyChemistFormula);
        return prefix + "/edit";
    }

    /**
     * 修改保存药剂配方
     */
    @RequiresPermissions("fantasy:formula:edit")
    @Log(title = "药剂配方", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(FantasyChemistFormula fantasyChemistFormula)
    {
        SysUser user = ShiroUtils.getSysUser();
        fantasyChemistFormula.setUpdateBy(user.getUserId().toString());
        fantasyChemistFormula.setUpdateTime(new Date());
        return toAjax(fantasyChemistFormulaService.updateFantasyChemistFormula(fantasyChemistFormula));
    }

    /**
     * 删除药剂配方
     */
    @RequiresPermissions("fantasy:formula:remove")
    @Log(title = "药剂配方", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(fantasyChemistFormulaService.deleteFantasyChemistFormulaByIds(ids));
    }
}
